package com.qububu.play.dineTogether.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.DineStaffDao;
import com.qububu.play.dineTogether.dto.StaffInfoDto;
import com.qububu.play.dineTogether.service.DineStaffService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Service
public class DineStaffServiceImpl implements DineStaffService {

	@Resource
	private DineStaffDao dineStaffDao;
	
	@Resource
	private Cache dineStaffCache;
	
	public boolean doAddStaffInfo(StaffInfoDto staffInfo) {
		/*int actionId = ParamConverter.convertToInteger(SystemUtil.decrypt(staffInfo.getActionId()) );
		if (removeElementFromCache(actionId)) {
			dineStaffDao.doAddStaffInfo(staffInfo);
			
			return true;
		} 
		
		return false;*/
		
		dineStaffDao.doAddStaffInfo(staffInfo);		
		return true;
	}

	public List<StaffInfoDto> getStaffListByActionId(String actionId) {
		int reActionId = ParamConverter.convertToInteger(SystemUtil.decrypt(actionId) );
		/*List<StaffInfoDto> staffList = (List<StaffInfoDto>) getElementFromCache(reActionId);
		if (staffList == null) {
			staffList = dineStaffDao.getStaffListByActionId(reActionId);
			doSaveElemToCache(reActionId, staffList);
		}*/
		
		List<StaffInfoDto> staffList = dineStaffDao.getStaffListByActionId(reActionId);
		return staffList;
	}

	public boolean doModifyStaff(StaffInfoDto staffInfo) {
		/*if (clearOfCache()) {
			dineStaffDao.modifyStaff(staffInfo);
			
			return true;
		}
		
		return false;*/
		
		dineStaffDao.modifyStaff(staffInfo);
		return true;
	}

	public boolean doDeleteStaff(String staffId) {
		int reStaffId = ParamConverter.convertToInteger(SystemUtil.decrypt(staffId) );
		/*if (clearOfCache()) {
			return dineStaffDao.deleteStaff(reStaffId);
		}
		
		return false;*/
		
		return dineStaffDao.deleteStaff(reStaffId);
	}
	
	private Object getElementFromCache(int key) {
		Element cacheElem = dineStaffCache.get(key);

		if (cacheElem != null) {
//			System.out.println("\n\n--------调试--[dineStaff]--[从缓存中[取出]一个元素]-------\n\n");
			return cacheElem.getObjectValue();
		}
		return null;
	}
	
	private void doSaveElemToCache(int key, Object elem) {
		Element foodElement = dineStaffCache.get(key);
		if (foodElement == null) {
			dineStaffCache.put(new Element(key, elem));
//			System.out.println("\n\n--------调试--[dineStaff]--[从缓存中[存入]一个元素]-------\n\n");
		}
	}

	// 清除指定key的缓存数据
	private boolean removeElementFromCache(int key) {
		if (dineStaffCache.get(key) != null) {
//			System.out.println("\n\n--------调试--[dineStaff]--[从缓存中[移除]一个元素]-------\n\n");
			return dineStaffCache.remove(key);	
		}
		return true;
	}
	
	/**
	 * 清空缓存中所有数据
	 * */
	private boolean clearOfCache() {
		if (dineStaffCache.getSize() > 0) {
			dineStaffCache.removeAll();
//			System.out.println("\n\n--------调试--[dineStaff]--[清空缓存数据]-------\n\n");
		}
		return true;
	}

}
